package com.yida.util;



import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;


public class utilOss {
	//外网访问节点
	public static String endpoint = "oss-cn-hangzhou.aliyuncs.com";
	//点击阿里云头像accesskey管理获取api权限
	public static String accesskeyid = "LTAI5t5m388seafcaTmn5BZJ";
	//密码
	public static String accesskeysecret = "8AdqkJf94ZyORMhM3EqvOuw9HkTTVQ";
	//bucket名
    public static String bucketname = "bloguser";
    //上传成功后返回得url
    public static String suffer_url = "http://bloguser.oss-cn-hangzhou.aliyuncs.com";
    //格式化日期用于文件名
   // private static SimpleDateFormat Formatwenjia = new SimpleDateFormat("yyyyMMdd_HH_mm_ssSSS");
    //格式化日期文件夹
    private static SimpleDateFormat Format = new SimpleDateFormat("yyyyMMdd");
    
    
    public static OSS getOss() {
    	
    	OSS ossClient = new OSSClientBuilder().build(endpoint, accesskeyid,accesskeysecret);
    	

    	if (ossClient.doesBucketExist(bucketname)) {
    	   //System.out.println("buck已存在");

		}else {
			//System.out.println("bucket不存在 ，创建bucket");
			CreateBucketRequest bucketRequest = new CreateBucketRequest(bucketname);
			//设置权限为公共读
            bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(bucketRequest);
		}

    	
    	return ossClient;
	}
    
    
    //上传文件
    
    public String uploadDocument(MultipartFile multipartFile,String businessTyps) {
    	
    	
    	
		//获取文件名
    	String ordname = multipartFile.getOriginalFilename();
    	//获取阿里云连接
    	OSS ossClient = this.getOss();
    	//获取后窜
    	String suffix = ordname.substring(ordname.lastIndexOf("."));
    	
    	String filename  =   getname(businessTyps,suffix);
    	
    	String url  = null;
    	
    	try {
    		
			ossClient.putObject(bucketname,filename, new ByteArrayInputStream(multipartFile.getBytes()));
		    
			url = suffer_url + "/" + filename ; 
			
			System.out.println("上传成功");
			
    	} catch (OSSException | ClientException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			
			ossClient.shutdown();
		}
    	
    	           
    	           
    	return url;
		
	}


	private String getname(String businessTyps,String suffix) {
		//日期
    	String date = Format.format(new Date());
    	
    	if (StringUtils.isEmpty(businessTyps)) { //判断类型是否为空
			businessTyps = "default";
		}
    	//为了避免图片重名 使用uuid命名图片
    	String uuid = UUID.randomUUID().toString().replace("-", "");
    			
    	//组合filename
    	String filename =  businessTyps+"/"+date+"/"+uuid+suffix;
    	
		return filename;
    	
	}
    
    
    
    
}
